package com.pinc.cpbl;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.pinc.cpbl.Menu_Adapter.generalClickListener;
import com.pinc.cpbl.Menu_Adapter.menu_class;

public class main extends WidgetActivity {
	private Menu_Adapter mAdapter;
	private ListView lv;
	private SharedPreferences.Editor editor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		initWidget();

		mAdapter = new Menu_Adapter(this);
		mAdapter.addMenuItem(new menu_class(getResources().getString(R.string.title_game_info), new generalClickListener(this, Recent_Game.class)));
		mAdapter.addMenuItem(new menu_class(getResources().getString(R.string.title_all_star), new generalClickListener(this, Allstar.class)));
		mAdapter.addMenuItem(new menu_class(getResources().getString(R.string.title_team_rank), new generalClickListener(this, show_rank.class)));
		mAdapter.addMenuItem(new menu_class(getResources().getString(R.string.title_schedule), new generalClickListener(this, schedule.class)));
		mAdapter.addMenuItem(new menu_class(getResources().getString(R.string.title_statistics), new generalClickListener(this, Statistics.class)));
		mAdapter.addMenuItem(new menu_class(getResources().getString(R.string.title_about), new generalClickListener(this, show_about.class)));


		lv.setAdapter(mAdapter);
		lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
				mAdapter.getClickListener(position).onClick(v);
			}
		});

		editor = PreferenceManager.getDefaultSharedPreferences(this).edit();

		Intent i = new Intent(this, BroadcastService.class);
		startService(i);
	}

	@Override
	protected void initUpbar() {
		tv_title.setText(R.string.app_name);

		b_right.setText(getResources().getString(R.string.str_setting));
		b_right.setOnClickListener(new generalClickListener(this, LiveSetting.class));

		b_left.setText(getResources().getString(R.string.str_exit));
	}

	@Override
	protected void initWidget() {
		super.initWidget();
		lv = (ListView)this.findViewById(R.id.listView1);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isNoInternet())
			return;
		new Thread(new Runnable(){
			@Override
			public void run() {
				Parser_Version pv = new Parser_Version();
				String appName = getPackageName().substring(getPackageName().lastIndexOf('.')+1);
				if (pv.parse(appName))
				{
					try {
						if (pv.getFeature().length()==2)
						{
							editor.putString(PreferenceHelper.CPBL_GAME_NO, pv.getFeature());
							editor.commit();
						}
						if ( getPackageManager().getPackageInfo(getPackageName(), 0).versionCode < pv.getVersion())
							main.this.runOnUiThread(new Runnable(){
								@Override
								public void run() {
									showDialog(ID_UPDATE);
								}
							});
					} catch (NameNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	private static final int ID_UPDATE = 1;

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch (id) {
		case ID_UPDATE: {
			builder.setMessage(
					getResources().getString(R.string.str_update_notify))
					.setPositiveButton(
							getResources().getString(R.string.str_update),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									Uri uri = Uri
									.parse(String.format("market://details?id=%s", getPackageName()));
									Intent intent_market = new Intent(
											Intent.ACTION_VIEW, uri);
									startActivity(intent_market);
								}
							})
							.setNegativeButton(
									getResources().getString(R.string.str_later), null);
			break;
		}
		default:
			break;
		}
		return builder.create();
	}
}
